<?php

namespace app\common\service;


use app\common\facade\oss\Upload;
use OSS\Core\OssException;
use OSS\OssClient;

class UploadService
{
    use ResponseService;

    /**
     * 表单上传
     * @param string $name
     * @return array|string
     */
    public function uploadForm($name, $path)
    {
        $file = request()->file($name);
        // 上传到本地服务器
        return \think\facade\Filesystem::disk('public')->putFile($path, $file);
    }

    /**
     * 表单上传
     * @param string $name
     * @return array|string
     */
    public function uploadOssForm($name, $path)
    {

        //获取配置
        $config = config('ali');
        $file = request()->file($name);

        $fileName = $path . time() . '.mp4';
        try {
            $OssClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);

            $uploadToAliyunOss = $OssClient->uploadFile($config['bucket'], $fileName, $file);

            return $uploadToAliyunOss['info']['url'];

        } catch (OssException $e) {
            halt($e->getMessage());
        }

    }


}